/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.sessionbean;

import cms.dataaccess.DAOFactory;
import dto.Reviewcategory;
import dto.Reviewcontents;
import dto.Reviewdetails;
import dto.Reviewlog;
import dto.Reviewrate;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;

/**
 *
 * @author Gary
 */
@Stateless(mappedName="ReviewMgmt")
public class ReviewMgmt implements ReviewMgmtRemote {

    @Override
    public String assignviewer(String conferenceName,String conferenceItem, String reviewer, String assigner, String title, String desc, String adminComments) {
        String uuid  = UUID.randomUUID().toString();
       String  cksql = "Reviewlog='"+conferenceName+"' and conferenceItemName='"+conferenceItem
                +"' and reviewer='"+reviewer+"' and assigner='"+assigner+"'";
        List<Reviewlog >  reviewCk=   DAOFactory.getInstance().getReviewLogDAO().findByCustomizeQuery(cksql);
       Reviewlog updateReview=reviewCk.get(0);
        if( updateReview==null){
           updateReview  =  new Reviewlog(uuid);;
       }
       updateReview.setAdminComments(adminComments);
         DateFormat formatter =new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
          java.util.Date dt = new java.util.Date();
          String currentTime = formatter.format(dt);
          Date assignDate;
        try {
            assignDate = formatter.parse(currentTime);
             updateReview.setAssignedTime(assignDate);
        } catch (ParseException ex) {
            System.err.println(ex.getMessage());
        }
            
            
          
           updateReview.setAssigner(assigner);
           updateReview.setReviewer(reviewer);
           updateReview.setConferenceItemName(conferenceItem);
           updateReview.setDescription(desc);
           updateReview.setTitle(title);
           updateReview.setConferenceName(conferenceName);
           
           DAOFactory.getInstance().getReviewLogDAO().updateReiewLog(updateReview);
        
            return uuid;
    }

    @Override
    public boolean createReviewerForm(String reviewId, int mark) {
       
        Reviewlog updateLog = DAOFactory.getInstance().getReviewLogDAO().getReviewlog(reviewId);
        if(updateLog == null){
            return false;
        }
        updateLog.setTotalMarks(mark);
        DAOFactory.getInstance().getReviewLogDAO().updateReiewLog(updateLog);
        return true;
    }
    @Override
    public Reviewlog getReviewLog(String reviewID) {
       return  DAOFactory.getInstance().getReviewLogDAO().getReviewlog(reviewID);
    }

    @Override
    public List<Reviewlog> getReviewLogs(String conferenceItemName) {
        
        String sql = "conferenceItemName='"+conferenceItemName+"'";
        return DAOFactory.getInstance().getReviewLogDAO().findByCustomizeQuery(sql);
    }

    @Override
    public List<Reviewlog> getReviewLogsByReviewer(String reviwer) {
          String sql = "reviewer='"+reviwer+"'";
        return DAOFactory.getInstance().getReviewLogDAO().findByCustomizeQuery(sql);
    }

    @Override
    public Reviewcategory getReviewCategory(String catID) {
        
        return DAOFactory.getInstance().getReviewCategoryDAO().getReviewCategory(catID);
    }


 
   
    
    
    
    @Override
    public boolean deleteReviewCategory(String catID) {
      Reviewcategory del= DAOFactory.getInstance().getReviewCategoryDAO().getReviewCategory(catID);
       DAOFactory.getInstance().getReviewCategoryDAO().deleteReviewCategory(del);
       return true;
    }

    @Override
    public boolean deleteReviewCategory(Reviewcategory category) {
      DAOFactory.getInstance().getReviewCategoryDAO().deleteReviewCategory(category);
      return true;
    }

    @Override
    public Reviewcontents getReviewcContents(String contentID) {
       return DAOFactory.getInstance().getReviewContentsDAO().getReviewContent(contentID);
    }

    @Override
    public List<Reviewcontents> getReviewcContentsByCategoryID(String catID) {
        Reviewcategory category  = getReviewCategory(catID);
          
       return    DAOFactory.getInstance().getReviewContentsDAO().getReviewContentByCategory(category.getCatName());
    }

    @Override
    public boolean updateReviewcContents(Reviewcontents content) {
        DAOFactory.getInstance().getReviewContentsDAO().updateReviewContents(content);
        return true;
    }

    @Override
    public boolean deleteReviewcContents(String contentID) {
        DAOFactory.getInstance().getReviewContentsDAO().deleteReviewContents(contentID);
        return true;
    }



   
    @Override
    public boolean updateReviewDetails(String catID, String contentID, int rate) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public boolean updateReviewCategory(String catName, int weight) {
       DAOFactory.getInstance().getReviewCategoryDAO().updateReviewCategory(catName, weight);
       return true;
    }

    @Override
    public List<Reviewcategory> getAllReviewCategory() {
      return DAOFactory.getInstance().getReviewCategoryDAO().getAllReviewCategories();
    }

    @Override
    public boolean updateReviewCategory(Reviewcategory category) {
       return DAOFactory.getInstance().getReviewCategoryDAO().updateReviewCategory(category);
    }
    

}
